//
// DepTreeSearcher
//
// Copyright (C) jextra.net.
//
//  This file is part of the net.jextra.dep Library.
//
//  The net.jextra.dep Library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  The net.jextra.dep Library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with the net.jextra.dep Library; if not, write to the Free
//  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
//  02111-1307 USA.
//

package net.jextra.dep;

import java.util.*;

/**
 * <p>
 * Collect useful information about a {@link DepTree}.
 * </p>
 */
public class DepTreeSearcher
{
    // ============================================================
    // Fields
    // ============================================================

    private ArrayList<Node> rootNodes;
    private HashSet<String> visitedNodes;
    private LinkedHashSet<DepComponent> components;
    private LinkedHashSet<DepComponentFile> allFiles;
    private LinkedHashSet<DepComponentFile> files;

    // ============================================================
    // Constructors
    // ============================================================

    public DepTreeSearcher( DepTree model )
        throws Exception
    {
        rootNodes = new ArrayList<Node>();
        visitedNodes = new HashSet<String>();
        components = new LinkedHashSet<DepComponent>();
        allFiles = new LinkedHashSet<DepComponentFile>();
        files = new LinkedHashSet<DepComponentFile>();

        for ( DepComponent component : model.getRootComponents() )
        {
            rootNodes.add( new Node( component ) );
        }

        boolean isRoot = true;
        for ( ArrayList<Node> level = rootNodes; !level.isEmpty(); )
        {
            ArrayList<Node> nextLevel = new ArrayList<Node>();

            for ( Node node : level )
            {
                nextLevel.addAll( processNode( node, isRoot ) );
            }

            isRoot = false;
            level = nextLevel;
        }
    }

    // ============================================================
    // Methods
    // ============================================================

    // ----------
    // public
    // ----------

    public Collection<DepComponent> getRootComponents()
    {
        ArrayList<DepComponent> list = new ArrayList<DepComponent>();

        for ( Node node : rootNodes )
        {
            list.add( node.getComponent() );
        }

        return list;
    }

    public Collection<DepComponentFile> getAllFilesIncludingSelf()
    {
        return allFiles;
    }

    public Collection<DepComponentFile> getFiles()
    {
        return files;
    }

    public Collection<DepComponent> getDependencies()
    {
        return components;
    }

    // ----------
    // private
    // ----------

    private Collection<Node> processNode( Node node, boolean isRoot )
    {
        boolean redundant = visitedNodes.contains( node.getName() );
        node.setRedundant( redundant );

        visitedNodes.add( node.getName() );

        if ( redundant )
        {
            return node.getChildren();
        }

        // TODO this is pom specific
//        if ( node.getComponent().getSeedDependency() != null
//            && "provided".equalsIgnoreCase( node.getComponent().getSeedDependency().getScope() ) )
//        {
//            return new ArrayList<Node>();
//        }

        for ( DepComponentFile file : node.getComponent().getFiles() )
        {
            allFiles.add( file );
            if ( !isRoot )
            {
                files.add( file );
                components.add( node.getComponent() );
            }
        }

        for ( DepComponent component : node.getComponent().getChildren() )
        {
            node.addChild( new Node( component ) );
        }

        return node.getChildren();
    }

    // ============================================================
    // Inner Classes
    // ============================================================

    private static class Node implements Comparable<Node>
    {
        private DepComponent component;
        private boolean redundant;
        private TreeSet<Node> children;

        public Node( DepComponent component )
        {
            this.component = component;
            children = new TreeSet<Node>();
        }

        public String getName()
        {
            return component.getName();
        }

        public DepComponent getComponent()
        {
            return component;
        }

        @SuppressWarnings( "unused" )
        public boolean isRedundant()
        {
            return redundant;
        }

        public void setRedundant( boolean redundant )
        {
            this.redundant = redundant;
        }

        public TreeSet<Node> getChildren()
        {
            return children;
        }

        public void addChild( Node child )
        {
            children.add( child );
        }

        @Override
        public int compareTo( Node other )
        {
            return getName().compareTo( other.getName() );
        }
    }
}
